'''789v5无人机操作'''
import random
from time import sleep
from devices import dv


def start_dragonfly():
    '''启动无人机'''
    dv.app_start('com.txzing.dragonfly')
    sleep(2)
    dv(text='Mini3').click()
    dv(text='确定').click()
    sleep(10)


def stop_dragonfly():
    '''关闭无人机'''
    dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/ll_back"]/android.widget.ImageView[1]').click()
    dv.xpath('//*[@resource-id="android:id/content"]/android.view.ViewGroup[1]/android.widget.FrameLayou'
             't[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.widget.ImageView[2]').click()
    dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/iv_home"]').click()
    dv(text='确定').click()


def lx():
    '''录像'''
    dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/widget_capture"]').click()  # 拍照录像按钮
    sleep(5)
    dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/widget_capture"]').click()  # 拍照录像按钮


def pz():
    '''拍照'''
    dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/widget_capture"]').click()  # 拍照录像按钮


def xc():
    num = 3  #
    pv = f'//*[@resource-id="com.txzing.dragonfly:id/rv_media"]/android.view.ViewGroup[{num}]/android.widget.ImageView[1]'
    dv.xpath(pv).click()  # 点击第几个图片或者视频
    if dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/tv_current_time"]').exists:
        '''视频操作'''
        button = dv.xpath(
            '//*[@resource-id="com.txzing.dragonfly:id/tb_play"]/android.widget.ImageView[1]')  # 播放暂停按钮
        for i in range(15):
            button.click()
        dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/iv_video_next"]/android.widget.ImageView[1]').click()  # 下一个

    else:
        '''照片操作'''
        dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/iv_photo"]').click()  # 进入相册
        next = '//*[@resource-id="com.txzing.dragonfly:id/fl_media"]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/and' \
               'roid.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.ImageView[1]'
        dv.xpath(next).click()  # 切换下一个


# xc()
# dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/tb_play"]/android.widget.ImageView[1]').click()  # 播放暂停按钮
# dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/iv_video_next"]/android.widget.ImageView[1]').click()  # 下一个
# start_dragonfly()
# lx()
# dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/widget_switch_mode"]').click()  # 切换拍照录像模式
# pz()
# xc()
# stop_dragonfly()
# xc()
def t():
    while True:
        dv.xpath('//*[@resource-id="com.android.systemui:id/ivApp"]').click()  # 点击菜单键
        start_dragonfly()
        dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/widget_switch_mode"]').click()
        lx()
        dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/widget_switch_mode"]').click()
        pz()
        stop_dragonfly()


while True:
    try:
        t()
    except:
        print('报错')

# dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/widget_switch_mode"]').click()  # 切换拍照录像模式
#

#
# dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/cb_camera_settings"]').click()  # 相机设置2

# dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/iv_ev_add"]').click()  # EV+
#
# dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/iv_ev_reduce"]').click()  # EV-

# dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/rbtn_picture"]').click()  # 拍照设置

# dv.xpath('//*[@resource-id="com.txzing.dragonfly:id/rbtn_record"]').click()  # 录像                                                              设置

# dv(text='照片比例').click()
# dv(text='白平衡').click()
# dv(text='曝光模式').click()
# dv(text='视频尺寸').click()

# dv(text='3840x2160').click()
# dv(text='1920x1080').click()
# dv(text='30FPS').click()

# bph = ['自动', '晴天', '阴天', '室内(白炽灯)', '	室内(荧光灯)']
# dv(text=bph[1]).click()  # 设置白平衡
# while True:
#     dv(text='机载闪存').click()
#     sleep(0.5)
#     dv(text='SD卡').click()


# previous = '//*[@resource-id="com.txzing.dragonfly:id/fl_media"]/android.view.ViewGroup[2]/android.view.ViewGroup[1]' \
#            '/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]'
# next = '//*[@resource-id="com.txzing.dragonfly:id/fl_media"]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/and' \
#        'roid.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.ImageView[1]'
# dv.xpath(previous).click()
# dv.xpath(next).click()
